

package com.yz.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yz.common.entity.*;
import com.yz.dao.PotteryDao;
import com.yz.dao.RuinDao;
import com.yz.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("potteryService")
public class PotteryServiceImpl extends ServiceImpl<PotteryDao,PotteryEntity> implements PotteryService {
    @Autowired
    CheckService checkService;
    @Autowired
    CameraService cameraService;
    @Autowired
    DrawingService drawingService;
    @Autowired
    ProjectService projectService;

    @Override
    public long selectId() {
        return this.baseMapper.selectId();
    }
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void postMe(PotteryForm data,UserEntity user) {
        PotteryEntity t = data.getPotteryEntity();
        CheckEntity check = new CheckEntity();
        check.setCreateAt(t.getUpdateAt());
        check.setCreateUser(t.getCreateUser());
        check.setName(projectService.selectById(t.getProjectId()).getName());
        check.setNameForm(contant.type_name_form[9]);
        check.setProjectId(t.getProjectId());
        check.setStatus(1);
        check.setType(5);
        check.setTypeForm(106);
        long id = 0;
        if(t.getId()==0){
            this.baseMapper.insert(t);
            id = this.baseMapper.selectId();
            check.setDataId(id);
            check.setNumber(contant.type_all_form[4]+contant.type_detail_form[9]+id);
            checkService.insert(check);
        }else{
            id = t.getId();
            check.setDataId(id);
            check.setNumber(contant.type_all_form[4]+contant.type_detail_form[9]+id);
            checkService.update(check,new EntityWrapper<CheckEntity>().eq("number",contant.type_all_form[4]+contant.type_detail_form[9]+id));
            this.baseMapper.update(t,new EntityWrapper<PotteryEntity>().eq("id",id));
        }
        //图片放入数据库
        cameraService.delete(new EntityWrapper<CameraEntity>().eq("data_id",id).eq("type_form",106));
        if(data.getCamera()!=null&&data.getCamera().size()>0){//照相号
            for (int i = 0; i <data.getCamera().size() ; i++) {
                CameraEntity item = data.getCamera().get(i);
                item.setPosterId(user.getUserId());
                item.setPostTime(t.getCreateAt());
                item.setCreateUser(user.getUsername());
                item.setProjectId(t.getProjectId());
                item.setTypeWhere(2);
                item.setDataId(id);
                item.setTypeForm(106);
                item.setFiledName("0");
                item.setNumber(contant.type_all_form[4]+contant.type_detail_form[9]+id);
                cameraService.insert(item);
            }
        }
        if(data.getDrawing()!=null&&data.getDrawing().size()>0){//绘图号
            drawingService.delete(new EntityWrapper<DrawingEntity>().eq("data_id",id).eq("type_form",106));
            for (int i = 0; i <data.getDrawing().size() ; i++) {
                DrawingEntity item1 = data.getDrawing().get(i);
                item1.setPosterId(user.getUserId());
                item1.setPostTime(t.getCreateAt());
                item1.setCreateUser(user.getUsername());
                item1.setProjectId(t.getProjectId());
                item1.setTypeWhere(2);
                item1.setDataId(id);
                item1.setTypeForm(106);
                item1.setNumber(contant.type_all_form[4]+contant.type_detail_form[9]+id);
                drawingService.insert(item1);
            }
        }
        if(data.getRubbing()!=null&&data.getRubbing().size()>0){//拓片号
            for (int i = 0; i <data.getRubbing().size() ; i++) {
                CameraEntity item = data.getRubbing().get(i);
                item.setPosterId(user.getUserId());
                item.setPostTime(t.getCreateAt());
                item.setCreateUser(user.getUsername());
                item.setProjectId(t.getProjectId());
                item.setTypeWhere(2);
                item.setDataId(id);
                item.setTypeForm(106);
                item.setFiledName("3");
                item.setNumber(contant.type_all_form[4]+contant.type_detail_form[9]+id);
                cameraService.insert(item);
            }
        }
        if(data.getLawTaoTireImage()!=null&&data.getLawTaoTireImage().size()>0){//淘汰示意图
            for (int i = 0; i <data.getLawTaoTireImage().size() ; i++) {
                CameraEntity item = data.getLawTaoTireImage().get(i);
                item.setPosterId(user.getUserId());
                item.setPostTime(t.getCreateAt());
                item.setCreateUser(user.getUsername());
                item.setProjectId(t.getProjectId());
                item.setTypeWhere(2);
                item.setDataId(id);
                item.setTypeForm(106);
                item.setFiledName("4");
                item.setNumber(contant.type_all_form[4]+contant.type_detail_form[9]+id);
                cameraService.insert(item);
            }
        }
        if(data.getLineDiagram()!=null&&data.getLineDiagram().size()>0){//器物线图
            for (int i = 0; i <data.getLineDiagram().size() ; i++) {
                CameraEntity item = data.getLineDiagram().get(i);
                item.setPosterId(user.getUserId());
                item.setPostTime(t.getCreateAt());
                item.setCreateUser(user.getUsername());
                item.setProjectId(t.getProjectId());
                item.setTypeWhere(2);
                item.setDataId(id);
                item.setTypeForm(106);
                item.setFiledName("5");
                item.setNumber(contant.type_all_form[4]+contant.type_detail_form[9]+id);
                cameraService.insert(item);
            }
        }
        if(data.getImages()!=null&&data.getImages().size()>0){//器物照片，拓片
            for (int i = 0; i <data.getImages().size() ; i++) {
                CameraEntity item = data.getImages().get(i);
                item.setPosterId(user.getUserId());
                item.setPostTime(t.getCreateAt());
                item.setCreateUser(user.getUsername());
                item.setProjectId(t.getProjectId());
                item.setTypeWhere(2);
                item.setDataId(id);
                item.setTypeForm(106);
                item.setFiledName("6");
                item.setNumber(contant.type_all_form[4]+contant.type_detail_form[9]+id);
                cameraService.insert(item);
            }
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deleteMe(long id) {
        cameraService.delete(new EntityWrapper<CameraEntity>().eq("data_id",id).eq("type_form",106));
        drawingService.delete(new EntityWrapper<DrawingEntity>().eq("data_id",id).eq("type_form",106));
        this.baseMapper.deleteById(id);
    checkService.deleteMe(5,106,id);
    }

    @Override
    public PotteryForm getDetails(long id) {
        PotteryForm data = new PotteryForm();
        PotteryEntity t = this.baseMapper.selectById(id);
        List<DrawingEntity> dds = drawingService.selectList(new EntityWrapper<DrawingEntity>().eq("data_id",id).eq("type_form",106));
        List<CameraEntity> cc1s = cameraService.selectList(new EntityWrapper<CameraEntity>().eq("filed_name","0").eq("data_id",id).eq("type_form",106));
        List<CameraEntity> cc2s = cameraService.selectList(new EntityWrapper<CameraEntity>().eq("filed_name","3").eq("data_id",id).eq("type_form",106));
        List<CameraEntity> cc3s = cameraService.selectList(new EntityWrapper<CameraEntity>().eq("filed_name","4").eq("data_id",id).eq("type_form",106));
        List<CameraEntity> cc4s = cameraService.selectList(new EntityWrapper<CameraEntity>().eq("filed_name","5").eq("data_id",id).eq("type_form",106));
        List<CameraEntity> cc5s = cameraService.selectList(new EntityWrapper<CameraEntity>().eq("filed_name","6").eq("data_id",id).eq("type_form",106));
        data.setDrawing(dds);
        data.setCamera(cc1s);
        data.setRubbing(cc2s);
        data.setLawTaoTireImage(cc3s);
        data.setLineDiagram(cc4s);
        data.setImages(cc5s);
        data.setPotteryEntity(t);
        return data;
    }
}
